/* 
 * Copyright (c) 2004-2005 Massachusetts Institute of Technology. This code was
 * developed as part of the Haystack (http://haystack.lcs.mit.edu/) research 
 * project at MIT. Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation files (the 
 * "Software"), to deal in the Software without restriction, including without 
 * limitation the rights to use, copy, modify, merge, publish, distribute, 
 * sublicense, and/or sell copies of the Software, and to permit persons to whom
 * the Software is furnished to do so, subject to the following conditions: 
 * 
 * The above copyright notice and this permission notice shall be included in 
 * all copies or substantial portions of the Software. 
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER  
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE. 
 */
/*
 * Created on Oct 30, 2004
 *
 */
package edu.mit.csail.relo.ui;

import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.LightweightSystem;
import org.eclipse.draw2d.parts.Thumbnail;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IEditorReference;
import org.eclipse.ui.IPartListener;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.part.ViewPart;

/**
 * @author vineet
 *
 * TODO: make this more of GEF like and have a viewer that the user can drag 
 *  around to pan in the main window
 */
public class ReloThumbnailView extends ViewPart {

    private IPartListener ipl = null;

    private Composite frame;
    private Canvas overviewCanvas;

    private ReloEditor currentEditor;

    /* (non-Javadoc)
     * @see org.eclipse.ui.IWorkbenchPart#createPartControl(org.eclipse.swt.widgets.Composite)
     */
    @Override
    public void createPartControl(Composite parent) {
        parent.setLayout(new FillLayout());
        
        frame = parent;
        
    	ipl = new IPartListener() {
            public void partActivated(IWorkbenchPart part) {
                if (part instanceof ReloEditor) {
                    setViewer((ReloEditor)part);
                }
            }
            public void partBroughtToTop(IWorkbenchPart part) {}
            public void partClosed(IWorkbenchPart part) {
                if (part instanceof ReloEditor && part == currentEditor) {
                    unsetViewer((ReloEditor)part);
                }
            }
            public void partDeactivated(IWorkbenchPart part) {}
            public void partOpened(IWorkbenchPart part) {}
        };
        getSite().getWorkbenchWindow().getPartService().addPartListener(ipl);

        IWorkbenchPage iap = getActivePage();
        if (iap == null) return;
        
        IEditorPart iep = iap.getActiveEditor();
        if (iep instanceof ReloEditor) {
            setViewer((ReloEditor)iep); 
        } else {
            // let's search for editor
            IEditorReference[] ier = iap.getEditorReferences();
            for (int i=0;i<ier.length;i++) {
                //System.err.println(ier[i].getId());
                if (ier[i].getId().equals(ReloEditor.editorId)) {
                    setViewer((ReloEditor)ier[i].getEditor(true));
                    break;
                }
            }
            
        }
    }
    
    private IWorkbenchPage getActivePage() {
        IWorkbench iwb = PlatformUI.getWorkbench();
        if (iwb==null)	return null;
        
        IWorkbenchWindow iww = iwb.getActiveWorkbenchWindow();
        if (iww==null)	return null;
        
        IWorkbenchPage iwp = iww.getActivePage();
        return iwp;
    }
    
    /* (non-Javadoc)
     * @see org.eclipse.ui.IWorkbenchPart#dispose()
     */
    @Override
    public void dispose() {
        super.dispose();
        getSite().getWorkbenchWindow().getPartService().removePartListener(ipl); 
    }
    

    /* (non-Javadoc)
     * @see org.eclipse.ui.IWorkbenchPart#setFocus()
     */
    @Override
    public void setFocus() {
    }


    private void setViewer(ReloEditor editor) {
        if (currentEditor != null) return;

        //System.err.println("Setting viewer to: " + editor);
        IFigure contentFig = editor.getContentsFigure();
        Thumbnail tn = new Thumbnail(contentFig);
        overviewCanvas = new Canvas(frame, SWT.NULL);
        overviewCanvas.setLayout(new FillLayout());
        LightweightSystem overview = new LightweightSystem(overviewCanvas);
        overview.setContents(tn);
        frame.layout();
        currentEditor = editor;
    }

    protected void unsetViewer(ReloEditor editor) {
        if (overviewCanvas != null) {
            overviewCanvas.dispose();
            frame.layout();
            overviewCanvas = null;
        } else {
            System.err.println("Canvas is already null!!");
        }
        currentEditor = null;
    }
}
